//使用文件I/O流将头像存入头像文件夹中，使注册成功的头像文件不被清理

package com.rope.backEnd.io;

import lombok.Getter;
import lombok.Setter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
@Getter
@Setter
public class FileCopyUtil {
    static String sourcePath;
    static String targetPath = "E:/ruanjianyingyong/Idea/JavaWJ/RopeNetsImg/img/avatar";
    /**
     * 复制文件从源路径到目标路径
     * @param sourcePath 源文件路径
     * @param targetPath 目标文件路径
     * @throws IOException 如果发生I/O错误
     */
    public static void copyFile(String sourcePath, String targetPath) throws IOException {

        File sourceFile = new File(sourcePath);
        File targetFile = new File(targetPath);
        if (!sourceFile.exists()) {
            throw new IOException("Source file does not exist: " + sourcePath);
        }
        try (FileChannel sourceChannel = new FileInputStream(sourceFile).getChannel();
             FileChannel targetChannel = new FileOutputStream(targetFile).getChannel()) {
            targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
        }
    }
    //获取文件名
    public static void avatarName(String AvatarName) throws IOException {
        sourcePath="E:/ruanjianyingyong/Idea/JavaWJ/RopeNetsImg/img/photos/"+AvatarName;

        System.out.println("转换前"+ sourcePath);

        FileCopyUtil.copyFile(sourcePath,targetPath+"/"+AvatarName);

        System.out.println("转换后"+ targetPath+"/"+AvatarName);

    }


}
